package me.mononin.factory.data.helper;

import android.text.TextUtils;

import me.mononin.common.factory.data.DataSource;
import me.mononin.factory.Factory;
import me.mononin.factory.R;
import me.mononin.factory.model.api.RspModel;
import me.mononin.factory.model.api.account.AccountRspModel;
import me.mononin.factory.model.api.account.LoginModel;
import me.mononin.factory.model.api.account.RegisterModel;
import me.mononin.factory.model.db.User;
import me.mononin.factory.net.Network;
import me.mononin.factory.net.RemoteService;
import me.mononin.factory.persistence.Account;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AccountHelper {

    /**
     * 用户注册接口，实现注册
     * @param model 用户数据
     * @param callback 回调
     */
    public static void register(RegisterModel model, final DataSource.Callback<User> callback){
        //调用retrofit对请求接口进行代理
        RemoteService service = Network.remote();
        //返回一个call
        Call<RspModel<AccountRspModel>> call = service.accountRegister(model);
        //请求响应结结果
        call.enqueue(new AccountRspCallback(callback));
    }

    /**
     * 登录的调用
     *
     * @param model    登录的Model
     * @param callback 成功与失败的接口回送
     */
    public static void login(final LoginModel model, final DataSource.Callback<User> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<AccountRspModel>> call = service.accountLogin(model);
        // 异步的请求
        call.enqueue(new AccountRspCallback(callback));
    }

    /**
     * 用户绑定设备Id
     * @param callback callback
     */
    public static void bindPush(final DataSource.Callback<User> callback){
        // 检查是否为空
        String pushId = Account.getPushId();
        if (TextUtils.isEmpty(pushId))
            return;

        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
        call.enqueue(new AccountRspCallback(callback));
    }


    public static class AccountRspCallback implements Callback<RspModel<AccountRspModel>>{

        private  DataSource.Callback<User> callback;

        public AccountRspCallback(final DataSource.Callback<User> callback) {
            this.callback = callback;
        }

        @Override
        public void onResponse(Call<RspModel<AccountRspModel>> call,
                               Response<RspModel<AccountRspModel>> response) {
            //请求成功时提取返回信息，由Gson解析数据
            RspModel<AccountRspModel> rspModel = response.body();
            //验证是否成功
            if (rspModel.success()){
                //获取整体信息
                AccountRspModel accountRspModel = rspModel.getResult();
                //获取用户信息
                User user = accountRspModel.getCard();
                //用户信息保存到数据库中
                DbHelper.save(User.class,user);
                //保存用户注册信息到本地
                Account.login(accountRspModel);
                //判断用户设备绑定状态
                if (accountRspModel.isBind()) {
                    //在当前设备已绑定的情况下，通知本地数据设备已绑定
                    Account.setIsBind(true);
                    //执行成功加载方法
                    callback.onDataLoaded(user);
                }else {
                    //callback.onDataLoaded(accountRspModel.getCard());
                    //绑定设备Id
                    bindPush(callback);
                }
            }else {
                //通过解析相应的错误code来指定对应错误信息资源ID
                Factory.decodeResponseCode(rspModel,callback);
            }
        }

        @Override
        public void onFailure(Call<RspModel<AccountRspModel>> call, Throwable t) {
            //请求失败
            callback.onDataNotAvailable(R.string.data_network_error);
        }
    }


}
